package com.lonely.fx.拖拽;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @date: 2022-07-28 12:40
 * @author: ztkj-hzb
 * @description:
 **/
public class 拖拽文件实现复制 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();


        HBox hBox = new HBox();
        hBox.setPrefSize(400, 400);
        hBox.setStyle("-fx-background-color: pink");

        ImageView imageView = new ImageView();
        imageView.setPreserveRatio(true);
        imageView.setFitWidth(400);

        AnchorPane.setTopAnchor(hBox, 50.0);
        AnchorPane.setLeftAnchor(hBox, 80.0);
        hBox.getChildren().addAll(imageView);
        anchorPane.getChildren().addAll(hBox);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(600);
        primaryStage.show();


        //进入hbox，设置边框颜色
        hBox.setOnDragEntered(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                //进入其中，设置边框颜色
                hBox.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, new CornerRadii(5), new BorderWidths(3))));
            }
        });


        //移出hbox范围，还原边框
        hBox.setOnDragExited(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                hBox.setBorder(null);
            }
        });

        hBox.setOnDragOver(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                //传递下去
                event.acceptTransferModes(event.getTransferMode());
            }
        });

        hBox.setOnDragDropped(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {

                Dragboard dragboard = event.getDragboard();

                System.out.println("url:" + dragboard.hasUrl());
                System.out.println("image:" + dragboard.hasImage());
                System.out.println("html:" + dragboard.hasHtml());

                if (dragboard.hasFiles()) {
                    List<File> files = dragboard.getFiles();
                    //这里只处理1个做案例
                    File file = files.get(0);

                    try {
                        Image image = new Image(new FileInputStream(file));
                        imageView.setImage(image);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }

                    return;
                }

                if (dragboard.hasUrl()) {
                    Image image = new Image(dragboard.getUrl());
                    imageView.setImage(image);
                    return;
                }


            }
        });


    }
}
